package com.cong.leetcode._30;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class Solution {
    String s;
    String[] words;
    public List<Integer> findSubstring(String s, String[] words) {
        int len = s.length();
        if(len <= 0 || 0 == words.length) {
            return new ArrayList<>(0);
        }
        this.s = s;
        this.words = words;

        List<Integer> res = new LinkedList<>();
        for(int i = 0;i < len;i++) {
            if (isSubSerial(i, new boolean[words.length])) {
                res.add(i);
            }
        }
        return res;
    }

    // 递归判断offset 开头的字符串是不是包含words串
    private boolean isSubSerial(int offset, boolean[] used) {
        System.out.println("offset: " + offset);
        if (offset > s.length()) return false;
        boolean startsWithOneWord = true;
        for(int i = 0;i < used.length;i++) {
            if (used[i]) continue;
            startsWithOneWord = false;
            if (s.startsWith(words[i], offset)) {
//                if(offset == 12 && words[i].equals("good")) System.out.println("fuck you");
                used[i] = true;
                return isSubSerial(offset + words[i].length(), used);
            }
        }
        if (offset == 12) {
//            System.out.println("???");
        }
        return startsWithOneWord;
    }
}